<?php 
class Base{
	public $mysql;
	public $initdb=false;
	public function Base(){
		if (!isset($_SESSION['username']) || empty($_SESSION['username'])){
			if (isset($_GET['a']) || isset($_GET['c'])){
				header('Location: ?login');
				exit;
			}
				
		}
	}
	public function db(){
		global $dbhost,$dbuser,$dbpwd,$dbName;
		$this->mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
		$this->mysqli->set_charset("utf8");
		$this->initdb=true;
	}
	public function fetch_array($sql){
		$query=$this->mysqli->query($sql);
		return $query->fetch_array();
	}
	public function query($sql){
		if (!$this->mysqli->query($sql)) {
			printf("query error: %s\n", $mysqli->error);
			exit;
		}
	}
	
	function __destruct() {
		if(true == $this->initdb){
			$this->mysqli->close();
		}
	}
}